const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
const {
  MemberModel
} = require('../../../../utils/member-model.js');
var memberModel = new MemberModel();
var encryption = require('../../../../utils/edsEncryption.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPassword: '', //旧密码
    newPassword: '', //新密码
    affirmNewPassword: '', //确认新密码
    hasPwd: null, //0未设置密码，1已设置密码
    memberId: '', //会员id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      hasPwd: options.hasPwd ? options.hasPwd.toString() : '0'
    })
    wx.setNavigationBarTitle({
      title: this.data.hasPwd == '1' ? "修改密码" : "设置支付密码"
    })
    this.getUserInfo(); //获取会员信息
  },
  /**
   * 旧密码
   */
  setOldPassword(e) {
    var value = e.detail.value;
    this.setData({
      oldPassword: value
    })
  },

  /**
   * 新密码
   */
  setNewPassword(e) {
    var value = e.detail.value;
    this.setData({
      newPassword: value
    })
  },

  /**
   * 确认新密码
   */
  setAffirmNewPassword(e) {
    var value = e.detail.value;
    this.setData({
      affirmNewPassword: value
    })
  },

  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      wx.setStorageSync('memberId', data.memberId);
      var that = this;
      that.setData({
        memberId: data.memberId ? data.memberId : "", //会员ID
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 动态获取加密密钥
   */
  getUserGetKey: function () {
    wx.reportAnalytics('gift_password_confirm');
    let that = this;
    giftShopModel.userGetKey({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      k: that.data.memberId, //会员id
    }).then(data => {
      that.setPassWord(data.secretKey)
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },


  /**
   * 设置密码
   */
  setPassWord: function (secretKey) {
    let that = this;
    let type = that.checkData()
    const shopInfo = wx.getStorageSync('shopInfo')
    if (type) {
      var param = {
        merchantId: wx.getStorageSync('merchantId'), //商户ID
        newPassword: encryption.encryptByDES(that.data.newPassword, secretKey, secretKey), //新密码
        shopId: shopInfo.shopId || '', //门店id
      }
      if (that.data.hasPwd == '1') {
        param.oldPassword = encryption.encryptByDES(that.data.oldPassword, secretKey, secretKey) //旧密码
        param.verify = 1 //0：重置   1：修改
      } else {
        param.verify = 0
      }
      giftShopModel.userPwdEdit(param).then(data => {
        if (that.data.hasPwd == '1') {
          wx.showToast({
            title: `密码已修改成功`,
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: `密码已设置成功`,
            icon: 'none',
            duration: 2000
          })
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 2
          })
        }, 1500)
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },

  /**
   * 数据检验
   */
  checkData: function () {
    let t = true
    let that = this;
    if (that.data.hasPwd == '1') {
      if (that.data.oldPassword.length != 6) {
        t = false
        wx.showToast({
          title: `请输入6位数字密码`,
          icon: 'none',
          duration: 2000
        })
      } else if (that.data.oldPassword == that.data.affirmNewPassword && that.data.newPassword == that.data.affirmNewPassword) {
        t = false
        wx.showToast({
          title: `新旧密码一致，设置失败`,
          icon: 'none',
          duration: 2000
        })
      }
    }
    if (that.data.newPassword.length != 6 || that.data.affirmNewPassword.length != 6) {
      t = false
      wx.showToast({
        title: `请输入6位数字密码`,
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.newPassword != that.data.affirmNewPassword) {
      t = false
      wx.showToast({
        title: `两次新密码输入不一致，请重新输入`,
        icon: 'none',
        duration: 2000
      })
    } else {
      t = true
    }
    return t;
  }
})